<template>
	<uni-popup ref="popup">
		<view class="transferInfo-box">
			<view class="box-head">
				<view class="box-title">{{title}}</view>
				<view class="iconfont iconclose icon-close" @click="closePopup"></view>
			</view>
			<view class="box-content">
				<view class="box-row" v-for="(item,index) in fields" :key="index">
					<view class="row-title">{{item.title}}</view>
					<view class="row-value">
						<view class="value">{{values[item.index]}}</view>
						<view class="copy" @click.stop="$util.copy(values[item.index])">复制</view>
					</view>
				</view>
			</view>
			<view class="box-footer">
				<view class="butt-stroke" @click="copyInfo">复制转账信息</view>
				<view class="butt-fill color-base-bg" @click="toPayResult">我已转账</view>
			</view>
		</view>
	</uni-popup>
</template>

<script>
	import uniPopup from '@/components/uni-popup/uni-popup.vue';
	export default {
		name: "ns-transfer-info",
		components: {
			uniPopup
		},
		props: {
			title: {
				type: String,
				default: () => {
					return '转账信息'
				}
			}
		},
		data() {
			return {
				out_trade_no: '',
				fields: [{
						title: '收款方姓名',
						index: 'receiveName',
					},
					{
						title: '收款方账号',
						index: 'receiveAccountNo'
					},
					{
						title: '收款方开户行',
						index: 'accountName'
					},
					{
						title: '省市/地区',
						index: 'areaInfo'
					},
					{
						title: '转账金额',
						index: 'amount'
					}
				],
				values: {
					receiveName: '',
					receiveAccountNo: '',
					accountName: '',
					areaInfo: '',
					amount: ''
				}
			};
		},
		methods: {
			openPopup(out_trade_no = '') {
				this.out_trade_no = out_trade_no;
				this.getTransferInfo();
				this.$refs.popup.open();
			},
			closePopup() {
				this.$refs.popup.close()
			},
			getTransferInfo() {
				return new Promise((resolve, reject) => {
					this.$api.sendRequest({
						url: '/api/pay/getTransferInfo',
						data: {
							out_trade_no: this.out_trade_no
						},
						success: (res) => {
							if (res.code == 0) {
								this.values = res.data;
								resolve(res.data)
							} else {
								reject(res.message)
							}
						},
						fail: () => {}
					})
				})
			},
			copyInfo() {
				let text = ''
				let items = []
				for (let i in this.fields) {
					let item = this.fields[i];
					let title = item.title;
					let value = this.values[item.index]
					items.push(`${title}: ${value}`)
				}
				text = items.join("\n");
				this.$util.copy(text)
			},
			toPayResult() {
				this.$util.redirectTo('/otherpages/pay/result/result', {
					code: this.out_trade_no
				}, 'redirectTo');
			}
		},
		created() {

		}
	}
</script>

<style lang="scss" scoped>
	%flex {
		display: flex;
		justify-content: center;
		align-items: center;
	}

	%butt {
		width: 330rpx;
		height: 80rpx;
		border-radius: 200rpx;

		font-size: 28rpx;
		line-height: 40rpx;
	}

	.transferInfo-box {
		position: fixed;
		bottom: 0;
		left: 0;
		background-color: #fff;
		width: 750rpx;
		// border-radius: 16rpx 16rpx 0 0;
		overflow: hidden;

		.box-head {
			@extend %flex;
			position: relative;
			height: 96rpx;
			background: #FFFFFF;
			border-bottom: 2rpx solid #EEEEEE;

			.box-title {
				font-size: 32rpx;
				font-weight: bold;
				color: #323233;
				line-height: 44rpx;
			}

			.icon-close {
				position: absolute;
				top: 28rpx;
				right: 32rpx;
				color: #C8C9CC;
				font-size: 32rpx;
			}
		}

		.box-content {
			padding: 0 32rpx;

			.box-row {
				@extend %flex;
				justify-content: space-between;
				margin-top: 24rpx;

				&:first-child {
					margin-top: 42rpx;
				}

				.row-title,
				.row-title {
					font-size: 28rpx;
					color: #323232;
					line-height: 40rpx;
				}

				.row-value {
					display: flex;
					align-items: center;

					.value {
						color: #999999;
					}

					.copy {
						margin-left: 12rpx;
						color: #1786f8;
						font-size: 24rpx;
						text-decoration: underline;
					}
				}
			}
		}

		.box-footer {
			@extend %flex;
			margin-top: 64rpx;
			padding-bottom: 24rpx;

			&.ios-safe {
				margin-bottom: 68rpx;
			}

			.butt-stroke {
				@extend %flex;
				@extend %butt;
				background: #FFFFFF;
				border: 2rpx solid #DCDEE0;
				color: #323233;
			}

			.butt-fill {
				@extend %flex;
				@extend %butt;
				margin-left: 24rpx;
				color: #fff;
			}
		}
	}
</style>